/*
* Copyright (C) 2024-2025 Uniontech Technology Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "privilegeseparationdialog.h"
#include "securitylevelchangedefinition.h"
#include "widgets/passwordedit.h"
#include "invokers/invokerfactory.h"

//#include <DBlurEffectWidget>
//#include <DLabel>
//#include <DSuggestButton>
//#include <DTitlebar>
//#include <DPasswordEdit>

//#include <DFontSizeManager>
//#include <QScroller>
#include <QScrollBar>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QChar>

#include <time.h>
#include <unistd.h>
#include <crypt.h>
#include <shadow.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <stdlib.h>

char *mkpasswd(const char *words)
{
    unsigned long seed[2];
    char salt[] = "$6$........";
    const char *const seedchars =
        "./0123456789ABCDEFGHIJKLMNOPQRST"
        "UVWXYZabcdefghijklmnopqrstuvwxyz";
    char *password;
    int i;

    // Generate a (not very) random seed. You should do it better than this...
    seed[0] = time(NULL);
    seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);

    // Turn it into printable characters from `seedchars'.
    for (i = 0; i < 8; i++) {
        salt[3 + i] = seedchars[(seed[i / 5] >> (i % 5) * 6) & 0x3f];
    }

    // DES Encrypt
    password = crypt(words, salt);

    return password;
}

PrivilegeSeparationDialog::PrivilegeSeparationDialog(QWidget *parent)
    : QFrame(parent)
    , m_sysadmPassword(nullptr)
    , m_sysadmPasswordRe(nullptr)
    , m_audadmPassword(nullptr)
    , m_audadmPasswordRe(nullptr)
    , m_secadmPassword(nullptr)
    , m_secadmPasswordRe(nullptr)
    , m_cancelBtn(nullptr)
    , m_oKBtn(nullptr)
    , m_isFinishFlag(false)
    , m_gSettingsInvokerInter(nullptr)
    , m_loginSafetySysInvokerInter(nullptr)
{
    m_gSettingsInvokerInter = InvokerFactory::GetInstance().CreateSettings(DEEPIN_DEFENDER_GSETTING_PATH, "", this);

    m_loginSafetySysInvokerInter = InvokerFactory::GetInstance().CreateInvoker("com.deepin.defender.LoginSafety",
                                                                               "/com/deepin/defender/LoginSafety",
                                                                               "com.deepin.defender.LoginSafety",
                                                                               ConnectType::SYSTEM, this);

    this->setAccessibleName("privilegeSeparationDialog");

    // 初始化UI
    initUI();
}

void PrivilegeSeparationDialog::initUI()
{
    // 设置为Dialog
    setWindowFlags(Qt::Dialog);
    // 设置为阻态
    setWindowModality(Qt::ApplicationModal);
    // 背景透明化
    setAttribute(Qt::WA_TranslucentBackground);

    setContentsMargins(0, 0, 0, 0);
    setFixedWidth(486);
    setFixedHeight(409);

    // 设置布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);
    setLayout(mainLayout);

//    // 背景模糊化
//    m_blurEffect = new DBlurEffectWidget(this);
//    m_blurEffect->setBlendMode(DBlurEffectWidget::BehindWindowBlend);
//    // 设置模糊大小
//    m_blurEffect->resize(size());
//    m_blurEffect->lower();

//    // 右上角关闭按钮
//    m_titleBar = new DTitlebar(this);
//    m_titleBar->setFixedHeight(40);
//    m_titleBar->setAccessibleName("privilegeSeparationDialog_closeTitleBar");
//    m_titleBar->setBackgroundTransparent(true);
//    m_titleBar->setMenuVisible(false);
//    mainLayout->addWidget(m_titleBar, 0, Qt::AlignRight | Qt::AlignTop);

    mainLayout->addWidget(initContent());
    mainLayout->addStretch(1);
}

QWidget *PrivilegeSeparationDialog::initContent()
{
    // 添加滑轮区域
    QScrollArea *scrollArea = new QScrollArea(this);
    scrollArea->setFrameShape(QFrame::Shape::NoFrame);
    scrollArea->setAccessibleName("privilegeSeparationDialog_scrollArea");
    scrollArea->viewport()->setAccessibleName("scrollArea_viewWidget");
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    QPalette pa = scrollArea->palette();
    pa.setColor(QPalette::ColorRole::Base, Qt::transparent);
    scrollArea->setContentsMargins(0, 0, 0, 0);
    scrollArea->setPalette(pa);
    scrollArea->setWidgetResizable(true);
    scrollArea->setLineWidth(0);
    scrollArea->setFixedWidth(width());
//    scrollArea->setFixedHeight(height() - m_titleBar->height());
    scrollArea->setFixedHeight(height());

//    // 触屏移动
//    QScroller::grabGesture(scrollArea->viewport(), QScroller::LeftMouseButtonGesture);
//    QScroller *scroller = QScroller::scroller(scrollArea);
//    QScrollerProperties sp;
//    sp.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
//    scroller->setScrollerProperties(sp);

    QWidget *contentWidget = new QWidget(this);
    contentWidget->setContentsMargins(20, 0, 20, 10);
    contentWidget->setFixedWidth(width());
    contentWidget->setAccessibleName("viewWidget_contentWidget");
    scrollArea->setWidget(contentWidget);

    // 内容布局
    QVBoxLayout *contentLayout = new QVBoxLayout(contentWidget);
    contentLayout->setContentsMargins(0, 0, 0, 0);
    contentLayout->setSpacing(0);
    contentWidget->setLayout(contentLayout);

    // 设置标题
    QHBoxLayout *titleLayout = new QHBoxLayout(contentWidget);
    titleLayout->setContentsMargins(0, 0, 0, 0);
    titleLayout->setSpacing(0);
    QLabel *title = new QLabel(this);
    title->setContentsMargins(20, 0, 0, 0);
    QFont font;
    font.setBold(true);
    title->setFont(font);
//    DFontSizeManager::instance()->bind(title, DFontSizeManager::T5); // 字体设置
    title->setAccessibleName("contentWidget_title");
    title->setText(tr("Configure privilege separation"));

    QLabel *tip = new QLabel(this);
    tip->setAccessibleName("contentWidget_tip");
    tip->setToolTip(tr("Privileges of administrators:\n1. System admin: create and delete users, install software, and make system settings;\n2. Security admin: modify security contexts of files, manage security contexts of users, query and set security configuration;\n3. Audit admin: add audit rules and view audit logs."));
    tip->setPixmap(QIcon::fromTheme(SECURITYLEVEL_NOTICE_ICON).pixmap(16, 16));
    tip->setContentsMargins(0, 0, 0, 0);
    titleLayout->addWidget(title);
    titleLayout->addSpacing(10);
    titleLayout->addWidget(tip);
    titleLayout->setAlignment(Qt::AlignCenter);
    contentLayout->addLayout(titleLayout);

    // 系统管理员 -- 设置
    contentLayout->addSpacing(10);
    QLabel *sysadmLabelone = new QLabel(tr("System admin"), this);
//    DFontSizeManager::instance()->bind(sysadmLabelone, DFontSizeManager::T7);
    sysadmLabelone->setContentsMargins(0, 0, 0, 0);
    sysadmLabelone->setWordWrap(true);
    sysadmLabelone->setAccessibleName("contentWidget_sysadmLabelUp");

    QLabel *sysadmLabelbottom1 = new QLabel("sysadm", this);
    sysadmLabelbottom1->setContentsMargins(0, 0, 0, 0);
//    DFontSizeManager::instance()->bind(sysadmLabelbottom1, DFontSizeManager::T8);

    m_sysadmPassword = new PasswordEdit(this);
    m_sysadmPassword->setContentsMargins(0, 0, 0, 0);
    m_sysadmPassword->setAccessibleName("contentWidget_sysadmPassword");
    m_sysadmPassword->setObjectName("sysadmPassword");
    m_sysadmPassword->setPlaceholderText(tr("Password"));
    contentLayout->addLayout(createPolicyItem(sysadmLabelone, sysadmLabelbottom1, m_sysadmPassword));
    contentLayout->addSpacing(5);

    QLabel *sysadmLabeltwo = new QLabel("", this);
    sysadmLabeltwo->setContentsMargins(0, 0, 0, 0);
    sysadmLabeltwo->setWordWrap(true);
    sysadmLabeltwo->setAccessibleName("contentWidget_sysadmLabelDown");

    QLabel *sysadmLabelbottom2 = new QLabel("", this);

    m_sysadmPasswordRe = new PasswordEdit(this);
    m_sysadmPasswordRe->setAccessibleName("contentWidget_sysadmPasswordRe");
    m_sysadmPasswordRe->setObjectName("sysadmPasswordRe");
    m_sysadmPasswordRe->setPlaceholderText(tr("Repeat password"));
    contentLayout->addLayout(createPolicyItem(sysadmLabeltwo, sysadmLabelbottom2, m_sysadmPasswordRe));

    // 审计管理员 -- 设置
    contentLayout->addSpacing(5);
    QLabel *audadmLabelone = new QLabel(tr("Audit admin"), this);
//    DFontSizeManager::instance()->bind(audadmLabelone, DFontSizeManager::T7);
    audadmLabelone->setWordWrap(true);
    audadmLabelone->setAccessibleName("contentWidget_audadmLabelUp");

    QLabel *audadmLabelbottom1 = new QLabel("audadm", this);
//    DFontSizeManager::instance()->bind(audadmLabelbottom1, DFontSizeManager::T8);

    m_audadmPassword = new PasswordEdit(this);
    m_audadmPassword->setAccessibleName("contentWidget_audadmPassword");
    m_audadmPassword->setObjectName("audadmPassword");
    m_audadmPassword->setPlaceholderText(tr("Password"));
    contentLayout->addLayout(createPolicyItem(audadmLabelone, audadmLabelbottom1, m_audadmPassword));
    contentLayout->addSpacing(5);

    QLabel *audadmLabeltwo = new QLabel("", this);
    audadmLabeltwo->setWordWrap(true);
    audadmLabeltwo->setAccessibleName("contentWidget_audadmLabelDown");

    QLabel *audadmLabelbottom2 = new QLabel("", this);

    m_audadmPasswordRe = new PasswordEdit(this);
    m_audadmPasswordRe->setAccessibleName("contentWidget_audadmPasswordRe");
    m_audadmPasswordRe->setObjectName("audadmPasswordRe");
    m_audadmPasswordRe->setPlaceholderText(tr("Repeat password"));
    contentLayout->addLayout(createPolicyItem(audadmLabeltwo, audadmLabelbottom2, m_audadmPasswordRe));

    // 安全管理员 -- 设置
    contentLayout->addSpacing(5);
    QLabel *secadmmLabelone = new QLabel(tr("Security admin"), this);
//    DFontSizeManager::instance()->bind(secadmmLabelone, DFontSizeManager::T7);
    secadmmLabelone->setWordWrap(true);
    secadmmLabelone->setAccessibleName("contentWidget_secadmmLabelUp");

    QLabel *secadmmLabelbottom1 = new QLabel("secadm", this);
//    DFontSizeManager::instance()->bind(secadmmLabelbottom1, DFontSizeManager::T8);

    m_secadmPassword = new PasswordEdit(this);
    m_secadmPassword->setAccessibleName("contentWidget_secadmPassword");
    m_secadmPassword->setObjectName("secadmPassword");
    m_secadmPassword->setPlaceholderText(tr("Password"));
    contentLayout->addLayout(createPolicyItem(secadmmLabelone, secadmmLabelbottom1, m_secadmPassword));
    contentLayout->addSpacing(5);

    QLabel *secadmmLabeltwo = new QLabel("", this);
    secadmmLabeltwo->setWordWrap(true);
    secadmmLabeltwo->setAccessibleName("contentWidget_secadmmLabelDown");

    QLabel *secadmmLabelbottom2 = new QLabel("", this);

    m_secadmPasswordRe = new PasswordEdit(this);
    m_secadmPasswordRe->setAccessibleName("contentWidget_secadmPasswordRe");
    m_secadmPasswordRe->setObjectName("secadmPasswordRe");
    m_secadmPasswordRe->setPlaceholderText(tr("Repeat password"));
    contentLayout->addLayout(createPolicyItem(secadmmLabeltwo, secadmmLabelbottom2, m_secadmPasswordRe));

//    // 密码规则限定
//    QRegularExpression PasswordJudegmetRegular = QRegularExpression("[\\w\\\\\\#\\~\\`\\.\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\-\\_\\+\\=\\|\\{\\}\\[\\]\\:\"\'\\<\\>\\,\?\\/]+");

//    m_sysadmPassword->lineEdit()->setValidator(new QRegularExpressionValidator(PasswordJudegmetRegular, m_sysadmPassword));
//    m_sysadmPasswordRe->lineEdit()->setValidator(new QRegularExpressionValidator(PasswordJudegmetRegular, m_sysadmPasswordRe));

//    m_audadmPassword->lineEdit()->setValidator(new QRegularExpressionValidator(PasswordJudegmetRegular, m_audadmPassword));
//    m_audadmPasswordRe->lineEdit()->setValidator(new QRegularExpressionValidator(PasswordJudegmetRegular, m_audadmPasswordRe));

//    m_secadmPassword->lineEdit()->setValidator(new QRegularExpressionValidator(PasswordJudegmetRegular, m_secadmPassword));
//    m_secadmPasswordRe->lineEdit()->setValidator(new QRegularExpressionValidator(PasswordJudegmetRegular, m_secadmPasswordRe));

    // 增加弹簧
    contentLayout->addStretch(1);

    // 设置底部按钮 --布局
    contentLayout->addSpacing(10);
    QHBoxLayout *bottomLayout = new QHBoxLayout;
    bottomLayout->setContentsMargins(0, 0, 0, 0);
    bottomLayout->setSpacing(0);
    contentLayout->addLayout(bottomLayout);

    // 设置底部 -- 取消按钮
    m_cancelBtn = new QPushButton(this);
    m_cancelBtn->setAccessibleName("contentWidget_cancelButton");
    m_cancelBtn->setText(tr("Cancel", "button"));
    m_cancelBtn->setObjectName("cancelButton");
    m_cancelBtn->setMinimumWidth(168);
    bottomLayout->addWidget(m_cancelBtn);
    connect(m_cancelBtn, SIGNAL(clicked()), this, SLOT(acceptCancelButtonClicked()));

    // 设置底部 -- 分隔线
    bottomLayout->addSpacing(10);
    QFrame *verticalLine = new QFrame(this);
    verticalLine->setAccessibleName("contentWidget_line");
    verticalLine->setFrameShape(QFrame::VLine);
    verticalLine->setFixedWidth(1);
    bottomLayout->addWidget(verticalLine);

    // 设置底部 -- 确定按钮
    bottomLayout->addSpacing(10);
    m_oKBtn = new QPushButton(this);
    m_oKBtn->setAccessibleName("contentWidget_okButton");
    m_oKBtn->setText(tr("Confirm", "button"));
    m_oKBtn->setObjectName("confirmButton");
    m_oKBtn->setMinimumWidth(168);
    bottomLayout->addWidget(m_oKBtn);
    connect(m_oKBtn, SIGNAL(clicked()), this, SLOT(acceptConfirmButtonClicked()));

    contentLayout->addStretch();
    return scrollArea;
}

QHBoxLayout *PrivilegeSeparationDialog::createPolicyItem(QWidget *leftWidget, QWidget *bottomWidget, QWidget *rightWidget)
{
    QHBoxLayout *itemLayout = new QHBoxLayout();
    itemLayout->setContentsMargins(0, 0, 0, 0);
    itemLayout->setSpacing(0);

    QVBoxLayout *itemVLayout = new QVBoxLayout();
    itemVLayout->setContentsMargins(0, 0, 0, 0);
    itemVLayout->setSpacing(0);

    if (!leftWidget) {
        leftWidget = new QWidget(this);
    }
    leftWidget->setContentsMargins(0, 0, 0, 0);
    bottomWidget->setContentsMargins(0, 0, 0, 0);
    rightWidget->setContentsMargins(0, 0, 0, 0);

    leftWidget->setFixedWidth(105);
    itemVLayout->addWidget(leftWidget, 0, Qt::AlignLeft);

    bottomWidget->setFixedWidth(105);
    itemVLayout->addWidget(bottomWidget, 0, Qt::AlignLeft);

    itemLayout->addLayout(itemVLayout);
    rightWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    itemLayout->addWidget(rightWidget);
    return itemLayout;
}

// 接受确认按钮槽
void PrivilegeSeparationDialog::acceptConfirmButtonClicked()
{
    // 判断密码框输入是否为空
    if (m_sysadmPassword->text().isEmpty()) {
        showTipErrorInfo(m_sysadmPassword, tr("Fill in the password please"));
        return;
    } else {
        //m_sysadmPassword->setAlert(false);
    }
    if (m_sysadmPasswordRe->text().isEmpty()) {
        showTipErrorInfo(m_sysadmPasswordRe, tr("Fill in the password please"));
        return;
    } else {
        //m_sysadmPasswordRe->setAlert(false);
    }
    if (m_audadmPassword->text().isEmpty()) {
        showTipErrorInfo(m_audadmPassword, tr("Fill in the password please"));
        return;
    } else {
        //m_audadmPassword->setAlert(false);
    }
    if (m_audadmPasswordRe->text().isEmpty()) {
        showTipErrorInfo(m_audadmPasswordRe, tr("Fill in the password please"));
        return;
    } else {
        //m_audadmPasswordRe->setAlert(false);
    }
    if (m_secadmPassword->text().isEmpty()) {
        showTipErrorInfo(m_secadmPassword, tr("Fill in the password please"));
        return;
    } else {
        //m_secadmPassword->setAlert(false);
    }
    if (m_secadmPasswordRe->text().isEmpty()) {
        showTipErrorInfo(m_secadmPasswordRe, tr("Fill in the password please"));
        return;
    } else {
        //m_secadmPasswordRe->setAlert(false);
    }

    // 判断密码长度是否超出限制
    if (PasswordJudgmentLength(m_sysadmPassword->text())) {
        showTipErrorInfo(m_sysadmPassword, tr("Password must be no more than %1 characters").arg("512"));
        return;
    } else {
        //m_sysadmPassword->setAlert(false);
    }

    if (PasswordJudgmentLength(m_audadmPassword->text())) {
        showTipErrorInfo(m_audadmPassword, tr("Password must be no more than %1 characters").arg("512"));
        return;
    } else {
        //m_audadmPassword->setAlert(false);
    }

    if (PasswordJudgmentLength(m_secadmPassword->text())) {
        showTipErrorInfo(m_secadmPassword, tr("Password must be no more than %1 characters").arg("512"));
        return;
    } else {
        //m_secadmPassword->setAlert(false);
    }

    // 判断系统管理员密码前后是否一致
    if (m_sysadmPassword->text() != m_sysadmPasswordRe->text()) {
        showTipErrorInfo(m_sysadmPasswordRe, tr("Passwords do not match"));
        return;
    } else {
        //m_sysadmPasswordRe->setAlert(false);
    }
    if (m_audadmPassword->text() != m_audadmPasswordRe->text()) {
        showTipErrorInfo(m_audadmPasswordRe, tr("Passwords do not match"));
        return;
    } else {
        //m_audadmPasswordRe->setAlert(false);
    }
    if (m_secadmPassword->text() != m_secadmPasswordRe->text()) {
        showTipErrorInfo(m_secadmPasswordRe, tr("Passwords do not match"));
        return;
    } else {
        //m_secadmPasswordRe->setAlert(false);
    }

    // 判断密码是否满足复杂度
    QDBusMessage msg = DBUS_BLOCK_INVOKE(m_loginSafetySysInvokerInter, "GetPwdLimitedLevel");
    GET_MESSAGE_VALUE(int, level, msg);
    int loginLevel = level - 1;
    if (loginLevel > getCurrentPasswordLevel(m_sysadmPasswordRe->text().toLatin1())) {
        if (SYSTEM_SAFE_LEVEL_MID == loginLevel) {
            showTipErrorInfo(m_sysadmPasswordRe, tr("At least %1 characters, and contain at least %2 of the four available character types: lowercase letters, uppercase letters, numbers, and symbols").arg(6).arg(2));
        } else if (SYSTEM_SAFE_LEVEL_HIGH == loginLevel) {
            showTipErrorInfo(m_sysadmPasswordRe, tr("At least %1 characters, and contain at least %2 of the four available character types: lowercase letters, uppercase letters, numbers, and symbols").arg(8).arg(3));
        }
        return;
    }
    if (loginLevel > getCurrentPasswordLevel(m_audadmPasswordRe->text().toLatin1())) {
        if (SYSTEM_SAFE_LEVEL_MID == loginLevel) {
            showTipErrorInfo(m_audadmPasswordRe, tr("At least %1 characters, and contain at least %2 of the four available character types: lowercase letters, uppercase letters, numbers, and symbols").arg(6).arg(2));
        } else if (SYSTEM_SAFE_LEVEL_HIGH == loginLevel) {
            showTipErrorInfo(m_audadmPasswordRe, tr("At least %1 characters, and contain at least %2 of the four available character types: lowercase letters, uppercase letters, numbers, and symbols").arg(8).arg(3));
        }
        return;
    }
    if (loginLevel > getCurrentPasswordLevel(m_secadmPasswordRe->text().toLatin1())) {
        if (SYSTEM_SAFE_LEVEL_MID == loginLevel) {
            showTipErrorInfo(m_secadmPasswordRe, tr("At least %1 characters, and contain at least %2 of the four available character types: lowercase letters, uppercase letters, numbers, and symbols").arg(6).arg(2));
        } else if (SYSTEM_SAFE_LEVEL_HIGH == loginLevel) {
            showTipErrorInfo(m_secadmPasswordRe, tr("At least %1 characters, and contain at least %2 of the four available character types: lowercase letters, uppercase letters, numbers, and symbols").arg(8).arg(3));
        }
        return;
    }

    Q_EMIT notifyDialogFinish(mkpasswd(m_sysadmPassword->text().toLatin1()), mkpasswd(m_secadmPassword->text().toLatin1()), mkpasswd(m_audadmPassword->text().toLatin1()));
    this->close();
}

// 接受取消按钮槽
void PrivilegeSeparationDialog::acceptCancelButtonClicked()
{
    m_isFinishFlag = true;

    Q_EMIT notifyDialogClose();
    this->close();
}

// 关闭窗口事件
void PrivilegeSeparationDialog::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    if (!m_isFinishFlag) {
        Q_EMIT notifyDialogClose();
    }
    deleteLater();
}

// 提示错误信息
void PrivilegeSeparationDialog::showTipErrorInfo(PasswordEdit *edit, const QString &msg)
{
//    edit->setAlert(true);
//    edit->showAlertMessage(msg, 3000);
//    connect(edit, &PasswordEdit::textChanged, this, [=] {
//        edit->setAlert(false);
//    });
}

// 得到当前输入密码等级
int PrivilegeSeparationDialog::getCurrentPasswordLevel(const char *newPasswd)
{
    int upper_count = 0;
    int lower_count = 0;
    int number_count = 0;
    int character_count = 0;
    int lenth;
    int type;
    char character;

    lenth = strlen(newPasswd);
    for (int i = 0; i < lenth; i++) {
        character = newPasswd[i];
        if (character >= 'a' && character <= 'z') {
            lower_count++;
        } else if (character >= 'A' && character <= 'Z') {
            upper_count++;
        } else if (character >= '0' && character <= '9') {
            number_count++;
        } else {
            character_count++;
        }
    }
    type = (upper_count > 0 ? 1 : 0) + (lower_count > 0 ? 1 : 0) + (number_count > 0 ? 1 : 0) + (character_count > 0 ? 1 : 0);

    if (lenth >= 8 && type >= 3) {
        return SYSTEM_SAFE_LEVEL_HIGH;
    } else if (lenth >= 6 && type >= 2) {
        return SYSTEM_SAFE_LEVEL_MID;
    } else {
        return SYSTEM_SAFE_LEVEL_LOW;
    }
}

// 判断输入密码字符长度是否超出限制
bool PrivilegeSeparationDialog::PasswordJudgmentLength(const QString &Passwd)
{
    int length;
    length = Passwd.length();
    return !(length <= 512);
}
